// $codepro.audit.disable packageNamingConvention, com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.alwaysOverridetoString.alwaysOverrideToString
/** Import Packages */
package edu.gatech.team404.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.gatech.team404.Course;
import edu.gatech.team404.dal.CourseDAL;
import edu.gatech.team404.tool.StatusCode;

/**
 * Servlet implementation class Registration
 * @author Michael Saul
 * @version 1.0
 */
@WebServlet("/Registration")
public class courseReg extends HttpServlet {

	/** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
	protected void processRequest(HttpServletRequest request, 
								  HttpServletResponse response)
								  throws IOException, NumberFormatException {
        response.setContentType("text/html;charset=UTF-8");
        final HttpSession session = request.getSession(true);
        final PrintWriter out = response.getWriter();
        final String statusTag = "accountCreationStatus";
        
        final CourseDAL dal = new CourseDAL();
        final String courseName = request.getParameter("courseName");
        
        try {
           final String crsName = request.getParameter("Courses");
           final String userName = request.getParameter("name");
           boolean isValid = true;
           
           //Validate username
           if (crsName.isEmpty()) {
        	   isValid = false;
        	   session.setAttribute(statusTag, 
        			   StatusCode.REGISTER_FAIL);
           }
           
           if (isValid) {
	           final Course course = dal.getCourseByName(crsName);

	           if (dal.signUpForCourse(userName, course)) {
	        	   isValid = true;
	        	   session.setAttribute(statusTag, StatusCode.REGISTER_SUCCESS);
	           }
	           else {
	        	   isValid = false;
	        	   session.setAttribute(statusTag, StatusCode.REGISTER_FAIL);
           }
           }
           
        }
        finally {
        	session.setAttribute("feed", StatusCode.CREATE.toString());
        	if (!courseName.equalsIgnoreCase("null")) {
        		response.sendRedirect("courseRegister.jsp?course=" 
            			+ URLEncoder.encode(courseName, "UTF-8"));
        	}
        	else {
        		response.sendRedirect("courseRegister.jsp");
        	}
        	dal.close();
        	out.close();
        }
    }

	/** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, NumberFormatException {
        processRequest(request, response);
    } 

	/** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, NumberFormatException {
        processRequest(request, response);
    }
    
}
